package org.lql.algo.codecrush.week009.question.type.I;

import org.lql.algo.common.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: liangqinglong
 * @date: 2025-08-20 16:48
 * @description: 589. N 叉树的前序遍历 <a href="https://leetcode.cn/problems/n-ary-tree-preorder-traversal/description/">...</a>
 **/
public class Preorder {

	/**
	 * * * 前序遍历：根-左-右
	 * * <p>
	 * * * 中序遍历：左-根-右
	 * * <p>
	 * * * 后序遍历：左-右-根
	 */
	public List<Integer> preorder(Node root) {
		List<Integer> res = new ArrayList<>();
		if (root == null) {
			return res;
		}
		dfs(res, root);
		return res;
	}

	private void dfs(List<Integer> res, Node root) {
		if (root == null) {
			return;
		}
		res.add(root.val);
		for (Node node : root.children) {
			dfs(res, node);
		}
	}

	public static void main(String[] args) {
		Preorder preorder = new Preorder();
		Node node1 = new Node(1, new ArrayList<>());
		Node node2 = new Node(3, new ArrayList<>());
		Node node3 = new Node(2, new ArrayList<>());
		Node node4 = new Node(4, new ArrayList<>());
		Node node5 = new Node(5, new ArrayList<>());
		Node node6 = new Node(6, new ArrayList<>());
		node2.children.add(node5);
		node2.children.add(node6);
		node1.children.add(node2);
		node1.children.add(node3);
		node1.children.add(node4);
		System.out.println(preorder.preorder(node1));
	}
}
